


def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(n ** 0.5) + 1, 2):
        if n % i == 0:
            return False
    return True


def largest_prime_from_digits(number):
    if len(str(number)) != 4:
        return 0

    digits = list(str(number))
    digits.sort(reverse=True)

    largest_prime = 0
    for i in range(4):
        for j in range(4):
            for k in range(4):
                for l in range(4):
                    if i != j and i != k and i != l and j != k and j != l and k != l:
                        new_number = int(digits[i] + digits[j] + digits[k] + digits[l])
                        if is_prime(new_number) and new_number > largest_prime:
                            largest_prime = new_number

    return largest_prime


# Ввод числа
number = input("Введите четырехзначное натуральное число: ")
try:
    number = int(number)
    result = largest_prime_from_digits(number)
    if result == 0:
        print(0)
    else:
        print(result)
except:
    print(0)
